package action.credit;

import model.credit.Applycustomrelation;

import model.credit.Creditapplypersonal;
import service.credit.ApplyCustomRelationInsertService;
import service.credit.CreditApplyPersonalService;
import util.Log;
import util.Time;
import util.Tool;
import core.action.BaseAction;

public class PersonalInforEditSaveAction extends BaseAction{
	
	private CreditApplyPersonalService creditApplyPersonalService;
	private ApplyCustomRelationInsertService applyCustomRelationInsertService;
	
	private String keepInforHidden;  //保证人编号
	private String hypothecateHidden;//抵押物品编号
	private String pledgeHidden;//质押物品编号
	
	private String keepInforHiddenRPT;//保证人类型
	private String hypothecateHiddenRPT;//抵押人类型
	private String pledgeHiddenRPT;//质押人类型
	
	private String hypothecateHiddenPI;//抵押人编号
	private String pledgeHiddenPI;//质押人编号
	
	
	private Integer applyid;
	private String applydate;
	private String loanuse;
	private String loanvarietie;
	private Double loanaccount;
	private String loanofmode;
	private int isAboutN;
	private int applytime;
	private String applytimetype;
	private String modeofrepayment;
	
	//private String sourceofrepayment;
	
	public String execute() throws Exception {
		
		Creditapplypersonal creditApplyPersonal = this.creditApplyPersonalService.getOneById(applyid);
		
		if(creditApplyPersonal != null){
			creditApplyPersonal.setApplydate(Time.DateString2Timestamp(applydate));
			creditApplyPersonal.setLoanuse(loanuse);
			creditApplyPersonal.setApplytime(applytime);
			creditApplyPersonal.setApplytimetype(applytimetype);
			creditApplyPersonal.setLoanaccount(loanaccount);
			creditApplyPersonal.setLoanofmode(loanofmode);
			creditApplyPersonal.setIsAboutN(isAboutN);
			creditApplyPersonal.setModeofrepayment(modeofrepayment);
		}
		
		//删除之前的所有抵质押信息
		this.applyCustomRelationInsertService.delete(applyid);
		
		String modes = creditApplyPersonal.getLoanofmode();
		
		if(!creditApplyPersonal.getLoanofmode().equals("4")){
			if(modes.contains("3") && this.keepInforHidden != null){
				String[] keepInforH = Tool.splitArrayHtml(keepInforHidden);
				String[] keepInforHRPT = Tool.splitArrayHtml(keepInforHiddenRPT);
				int[] keepInforHRPTi = this.c2i(keepInforHRPT);
				for(int i = 0; i < keepInforH.length; i++){
					Applycustomrelation www = new Applycustomrelation();
					www.setApplyid(creditApplyPersonal.getApplyid());
					www.setClientid(creditApplyPersonal.getClientid());
					www.setClienttype(1);
					www.setRelationtype(3);
					www.setRelationpeopleid(Integer.parseInt(keepInforH[i]));
					www.setRelationpeopletype(keepInforHRPTi[i]);
					www.setState("1");
					applyCustomRelationInsertService.save(www);
				}
				creditApplyPersonal.setModeB(1);
			}
			if(modes.contains("1") && this.hypothecateHidden != null){
				String[] hypothecateH = Tool.splitArrayHtml(hypothecateHidden);
				String[] hypothecateHRPT = Tool.splitArrayHtml(hypothecateHiddenRPT);
				int[] hypothecateHRPTi = this.c2i(hypothecateHRPT);
				String[] hypothecateHPI = Tool.splitArrayHtml(hypothecateHiddenPI);
				for(int i = 0; i < hypothecateH.length; i++){
					Applycustomrelation www = new Applycustomrelation(creditApplyPersonal.getApplyid(), creditApplyPersonal.getClientid(), 1, 1,  Integer.parseInt(hypothecateHPI[i]),  hypothecateHRPTi[i],  Integer.parseInt(hypothecateH[i]));
					www.setState("1");
					applyCustomRelationInsertService.save(www);
				}
				creditApplyPersonal.setModeD(1);
			}
			if(modes.contains("2") && this.pledgeHidden != null){
				String[] pledgeH = Tool.splitArrayHtml(pledgeHidden);
				String[] pledgeHRPT = Tool.splitArrayHtml(pledgeHiddenRPT);
				int[] pledgeHRPTi = this.c2i(pledgeHRPT);
				String[] pledgeHPI = Tool.splitArrayHtml(pledgeHiddenPI);
				for(int i = 0; i < pledgeH.length; i++){
					Applycustomrelation www = new Applycustomrelation(creditApplyPersonal.getApplyid(), creditApplyPersonal.getClientid(), 1, 2,  Integer.parseInt(pledgeHPI[i]),  pledgeHRPTi[i],  Integer.parseInt(pledgeH[i]));
					www.setState("1");
					applyCustomRelationInsertService.save(www);
				}
				creditApplyPersonal.setModeZ(1);
			}
			}else{
				creditApplyPersonal.setModeX(1);
			}
		this.creditApplyPersonalService.save(creditApplyPersonal);
		Log.save("更新操作", "修改申请号为：" + creditApplyPersonal.getApplyid() + "的个人的申请信息", "对应id编号为：" + creditApplyPersonal.getId());
		MessageJump("贷款申请信息修改成功", "creditSurveyAction?type=1");
		return "message";
	}
	
	
	public int[] c2i(String[] c){
		int[] ilist = new int[c.length];
		for(int i = 0; i < c.length; i++){
			if(c[i].equals("个人")){
				ilist[i] = 1;
			}else if(c[i].equals("企业")){
				ilist[i] = 2;
			}
		}
		return ilist;
	}


	public CreditApplyPersonalService getCreditApplyPersonalService() {
		return creditApplyPersonalService;
	}


	public void setCreditApplyPersonalService(
			CreditApplyPersonalService creditApplyPersonalService) {
		this.creditApplyPersonalService = creditApplyPersonalService;
	}


	public ApplyCustomRelationInsertService getApplyCustomRelationInsertService() {
		return applyCustomRelationInsertService;
	}


	public void setApplyCustomRelationInsertService(
			ApplyCustomRelationInsertService applyCustomRelationInsertService) {
		this.applyCustomRelationInsertService = applyCustomRelationInsertService;
	}


	public String getKeepInforHidden() {
		return keepInforHidden;
	}


	public void setKeepInforHidden(String keepInforHidden) {
		this.keepInforHidden = keepInforHidden;
	}


	public String getHypothecateHidden() {
		return hypothecateHidden;
	}


	public void setHypothecateHidden(String hypothecateHidden) {
		this.hypothecateHidden = hypothecateHidden;
	}


	public String getPledgeHidden() {
		return pledgeHidden;
	}


	public void setPledgeHidden(String pledgeHidden) {
		this.pledgeHidden = pledgeHidden;
	}


	public String getKeepInforHiddenRPT() {
		return keepInforHiddenRPT;
	}


	public void setKeepInforHiddenRPT(String keepInforHiddenRPT) {
		this.keepInforHiddenRPT = keepInforHiddenRPT;
	}


	public String getHypothecateHiddenRPT() {
		return hypothecateHiddenRPT;
	}


	public void setHypothecateHiddenRPT(String hypothecateHiddenRPT) {
		this.hypothecateHiddenRPT = hypothecateHiddenRPT;
	}


	public String getPledgeHiddenRPT() {
		return pledgeHiddenRPT;
	}


	public void setPledgeHiddenRPT(String pledgeHiddenRPT) {
		this.pledgeHiddenRPT = pledgeHiddenRPT;
	}


	public String getHypothecateHiddenPI() {
		return hypothecateHiddenPI;
	}


	public void setHypothecateHiddenPI(String hypothecateHiddenPI) {
		this.hypothecateHiddenPI = hypothecateHiddenPI;
	}


	public String getPledgeHiddenPI() {
		return pledgeHiddenPI;
	}


	public void setPledgeHiddenPI(String pledgeHiddenPI) {
		this.pledgeHiddenPI = pledgeHiddenPI;
	}


	public Integer getApplyid() {
		return applyid;
	}


	public void setApplyid(Integer applyid) {
		this.applyid = applyid;
	}


	public String getApplydate() {
		return applydate;
	}


	public void setApplydate(String applydate) {
		this.applydate = applydate;
	}


	public String getLoanuse() {
		return loanuse;
	}


	public void setLoanuse(String loanuse) {
		this.loanuse = loanuse;
	}


	public String getLoanvarietie() {
		return loanvarietie;
	}


	public void setLoanvarietie(String loanvarietie) {
		this.loanvarietie = loanvarietie;
	}


	public Double getLoanaccount() {
		return loanaccount;
	}


	public void setLoanaccount(Double loanaccount) {
		this.loanaccount = loanaccount;
	}


	public String getLoanofmode() {
		return loanofmode;
	}


	public void setLoanofmode(String loanofmode) {
		this.loanofmode = loanofmode;
	}


	public int getIsAboutN() {
		return isAboutN;
	}


	public void setIsAboutN(int isAboutN) {
		this.isAboutN = isAboutN;
	}


	public int getApplytime() {
		return applytime;
	}


	public void setApplytime(int applytime) {
		this.applytime = applytime;
	}


	public String getApplytimetype() {
		return applytimetype;
	}


	public void setApplytimetype(String applytimetype) {
		this.applytimetype = applytimetype;
	}


	public String getModeofrepayment() {
		return modeofrepayment;
	}


	public void setModeofrepayment(String modeofrepayment) {
		this.modeofrepayment = modeofrepayment;
	}

}
